package com.cwj.mansysback.service.impl;

import com.cwj.mansysback.dao.api.AccountDao;
import com.cwj.mansysback.dao.api.TeacherDao;
import com.cwj.mansysback.dao.impl.AccountDaoImpl;
import com.cwj.mansysback.dao.impl.TeacherDaoImpl;
import com.cwj.mansysback.entity.Account;
import com.cwj.mansysback.entity.Pack;
import com.cwj.mansysback.entity.Teacher;
import com.cwj.mansysback.service.api.TeacherService;

import java.util.List;

public class TeacherServiceImpl implements TeacherService {

    TeacherDao teaDao = new TeacherDaoImpl();

    AccountDao accDao = new AccountDaoImpl();


    @Override
    public List<Teacher> selectSomeTeacher(int start, int sum, String orderKey, boolean isAse) {
        return teaDao.selectTeacher(start, sum, orderKey, isAse);
    }


    @Override
    public boolean addTeaTeacher(Teacher teacher) {

        boolean flag = accDao.addAccount(teacher);

        if (!flag) {
            return false;
        }
        flag = teaDao.addTeacher(teacher.getAid(), teacher.getTname(), teacher.getTsalary(), teacher.getRid(), teacher.getTage(), teacher.getTsex());

        if (!flag) {
//            回滚
            accDao.deleteAccount(teacher);
            return false;
        }
        return true;
    }

    @Override
    public boolean deleteTeacher(String id) {
        boolean flag = true;
        Account acc = accDao.getAccount(id, Teacher.getRANK());
        flag = accDao.deleteAccount(id, Teacher.getRANK());

        if (!flag) return false;

        flag = teaDao.deleteTeacher(id);

        if (!flag) {
//            回滚
            accDao.addAccount(acc);
        }

        return flag;
    }

    @Override
    public Pack selectAllPack() {
        return teaDao.selectAllPack();
    }




    @Override
    public Teacher getTeacher(String tid) {
        return teaDao.selectTeacher(tid);
    }


    public TeacherDao getTeaDao() {
        return teaDao;
    }

    public void setTeaDao(TeacherDao teaDao) {
        this.teaDao = teaDao;
    }

    @Override
    public boolean updateTeacher(Teacher tea) {
        boolean flag = teaDao.updateTeacher(tea.getAid(), tea.getTname(), tea.getTsalary(), tea.getRid(), tea.getTsex(), tea.getTage());
        Teacher.updateStatic(flag);
        return flag;
    }

}
